#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>

int i,j,num,size=0,pos;
int* a;
int* b;
int flag,cnt,inde_x;
int temp;
char ans;
int in_size,ch;
int pop_n;
int cntr,k,size2;
int flag1;

//appends the given number at the end of the list
void append(int num)
{ a=realloc(a,(in_size+1)*sizeof(*a));
  a[in_size]=num;
  in_size++;
}

//appends a list of numbers at the end of the current list
void extend(int size2,int *b) 
{ a=realloc(a,(size+size2)*sizeof(*a));
  for(k=0;k<size2;k++)
	{ a[size+k]=b[k];
	}

}


//inserts a number at the given position
void insert(int num,int pos)
{ if(pos<=size) 
 	{a=realloc(a,(size+1)*sizeof(*a));
  	for(i=size;i>pos;i--)
		{ a[i]=a[i-1];
		}
   	a[pos]=num;
   	size++;
	}
 else
	{ printf("Enter valid position!!");
	}
}


//removes a given number from the list
void remov(int num)
{ flag=0;
  for (i=0;i<size;i++)
	{ if (a[i]==num)
	  	{flag=1; 
	   	break;}
	}
  if(flag==1)
   { i=i+1;
     while(i<size)
	 { a[i-1]=a[i];
	   i++;
	 }
      size --;
     a=realloc(a,size*sizeof(*a));
    }
  else
	{ printf("number not found!!!");
	}

}

//deletes the number at a given position and returns the deleted number
int  pop(int pos)
{ int n1;
  if(pos<size)
	{n1=a[pos];
  	i=pos+1;
     	while(i<size)
		 { a[i-1]=a[i];
		   i++;
		 }
      	size --;
  	return n1;
	}
  else
	{ printf("Enter valid position!!!");
	}
} 

//counts the no. of occurence of a number
int count(int num)
{ cnt=0;
  for (i=0;i<size;i++)
	{ if (a[i]==num)
		cnt++;
	}
  return cnt;
}

//returns the index of the given number
int indx(int num)
{ flag1=0;
  for (i=0;i<size;i++)
	{ if (a[i]==num)
		{ flag1=1;
		  return i;
		  break;
		}
	}
  if(flag1==0)
	{ printf("number not in the list...");
	  return -1;
	}
  
}
  
//sorts the elements 
void sort(int cntr)
{ 
  
  if(cntr==0)		//sorts in increasing order
  { for (i=0;i<size;i++)
	{ for(j=0;j<(size-1)-i;j++)
		{ if(a[j]>a[j+1])
			{ temp=a[j];
			  a[j]=a[j+1];
			  a[j+1]=temp;
			}
		}
	}
    printf("Sorting the numbers...");
   }
   else if (cntr==1)		//sorts in decreasing order
   { for (i=0;i<size;i++)
	{ for(j=0;j<(size-1)-i;j++)
		{ if(a[j]<a[j+1])
			{ temp=a[j];
			  a[j]=a[j+1];
			  a[j+1]=temp;
			}
		}
	}
     printf("Sorting the numbers...");
   } 
   else
	{ printf("Wrong choice entered!! ");
	}

}


//reverses the list
void reverse()	
{ printf("Reversing the numbers...");
  for (i=0,j=size-1;i<size/2;i++,j--)
	{ temp=a[i];
	  a[i]=a[j];
	  a[j]=temp;
	}
}

//prints the elements of the list
void print()
{printf("printing the numbers...");
 for(i=0;i<size;i++)
  { printf("\n %d \n",a[i]);
  }
}

void main()
{ printf("how many do you want to enter?");
  scanf("%d",&size);
  a=malloc(size*sizeof(*a));
  for (i=0;i<size;i++)
	{ printf("Enter :");
	  scanf("%d",&num);
	  append(num);
	}  
  
  printf("\t \t MENU");
  printf("\n 1. append...");
  printf("\n 2. extend...");
  printf("\n 3. insert...");
  printf("\n 4. delete...");
  printf("\n 5. pop...");
  printf("\n 6. count...");
  printf("\n 7. index...");
  printf("\n 8. sort...");
  printf("\n 9. reverse...");
  printf("\n 10. size...");
  printf("\n 11. display...");

  do
      { printf("\n Enter your choice...");
	scanf("%d",&ch);
  	switch(ch)
	{ case 1: { do	
			{ printf("\n Enter :");
	  		  scanf("%d",&num);
	  		  append(num);
	  		  size++;
	  		  printf("\n Do you want to enter more numbers?");
	  		  scanf("%s",&ans);
			}while(ans=='y' || ans=='Y');
		   print();
		  }
		 break;

	  case 2: { printf("how many do you want to enter?");
  		    scanf("%d",&size2);
 		    b=malloc(size2*sizeof(*b));
		    for (i=0;i<size2;i++)
			{ printf("Enter :");
	  		  scanf("%d",&b[i]);
			}
		    	
	  	    extend(size2,b);
	  	    size=size+size2;
	  	    print();
		  }
		 break;


  	  case 3: { do	
			{ printf("\n Enter the number and position:");
  			  scanf("%d %d",&num,&pos);
  			  insert(num,pos);
 			  printf("\n Do you want to enter more numbers?");
	  		  scanf("%s",&ans);
			}while(ans=='y' || ans=='Y');
		   print();
		  }
		 break;
	
	  case 4: { printf("\n Enter the number which you want to remove:");
  		    scanf("%d",&num);
  		    remov(num);
		    print();
  		  }		
		  break;		
	
	  
	  case 5: { printf("\n Enter the position from which you want to remove the number:");
  		    scanf("%d",&pos);
		    if(pos<size)
  		     { pop_n=pop(pos);
		       printf("\n The deleted number is: %d",pop_n);
		     }
		    else
			{ printf("\n Enter valid position! ");
		        }
		    print();
  		  }		
		  break;		
		
	  case 6: { do	
			{ printf("\n Enter the number you want to count:");
  			  scanf("%d",&num);
  			  cnt=count(num);
                          printf("\n The number occurs %d times",cnt);
			  printf("\n Do you want to count more numbers?");
	  		  scanf("%s",&ans);
			}while(ans=='y' || ans=='Y');
		   print();
		  }
		 break;
  	
	  case 7: { do	
			{ 
				printf("\n Enter the number whose index you want to search:");
  				scanf("%d",&num);
  				inde_x=indx(num);
  				printf("\n The number occurs  at %d place",inde_x);
				printf("\n Do you want to search index of more numbers?");
	  		  	scanf("%s",&ans);
			}while(ans=='y' || ans=='Y');
		   print();
		  }
		 break;
  
 	  case 8:{ printf("\n You want to sort the list in increasing(0) or decreasing order(1)?");
  		   printf("\n Enter choice 0 or 1:");
  		   scanf("%d",&cntr);	
		   sort(cntr);
		   print();
		 }
	       break; 
  
	  case 9:{ reverse();
		   print();
		 }
	      break;
	  
	case 10:{  printf("Size of the list is:%d",size);
		 }
	      break;


          case 11:{ 
		    print();
		 }
	      break;
    	    	

	  default : printf("\n Wrong choice entered");

        }
     printf("\n do you want to perform more operations?");
     scanf("%s",&ans);
  }while(ans=='y' || ans=='Y');
  
  
}
